<?php
/**
 * --------------------------------------------------------------------
 * MCore框架
 * 作者：阿枫，QQ：52037872，Email：nczy520@gmail.com
 * --------------------------------------------------------------------
 *
 * SVN revision information:
 * @version $Revision: 50 $:
 * @author  $Author: zy $:
 * @date      $Date: 2013-11-09 19:34:15 +0800 (星期六, 09 十一月 2013) $:
 * @url         $HeadURL: svn://vps.mdeve.com/web_mcore/manage/function/global.func.php $:
 */

class _ModulesClass extends __basic_def {

    function _list(){
        global $_M;

        $randstr = create_random_str(32);
        $_SESSION["tamperproof_verify_code"] = $randstr;

        $_M->file();

        // 获取本地所有模块
        $disk_modules = array();
        $folders = $_M->file->seek_file(".*", MODULE_PATH, 0, 1, 99);
        if(is_array($folders) && sizeof($folders)) {
            foreach ($folders as $folder) {
                // 判断是否为插件目录
                $location = $folder["location"] . DIRECTORY_SEPARATOR . "module.php";
                if (file_exists($location)) {
                    $disk_modules[] = array(
                        "name" => $folder["name"],
                        "intro" => $_M->module->get_intro($folder["name"]),
                        "version" => $_M->module->get_version($folder["name"]),
                        "installed" => in_array($folder["name"], $_M->module_installed) ? "<span class='text-success'>已安装</span>" : "<span class='text-danger'>未安装</span>",
                        "display_add" => in_array($folder["name"], $_M->module_installed) ? "none" : "inherit",
                        "display_remove" => in_array($folder["name"], $_M->module_installed) ? "inherit" : "none",
                    );
                }
            }
        }

        $breadcrumb = array(
            "系统设置" => "#",
            "系统模块管理" => "#",
            "模块列表" => "",
        );

        $_M->template();
        $_M->template->load(MANAGE_PATH. "/templates/options/modules_list");
        $_M->template->process_block("list", $disk_modules);
        $_M->template->set_var("{tamperproof_verify_code}", $randstr);
        $_M->template->insert_html(_generate_breadcrumb($breadcrumb));
        $_M->template->display();
    }

    private function _options_data_defined(){
        return $data = array(
            "type" => "system",
            "options_name" => "module_installed",
            "options_value" => json_encode(array()),
            "info" => "系统已安装插件列表",
            "category" => "hidden",
            "field" => "textarea",
        );
    }

    function _install() {
        global $_M;

        $code = filter_input(INPUT_POST, "code");
        if($code != $_SESSION["tamperproof_verify_code"]) ajax_result("error", "验证错误！");
        $name = clear_string(filter_input(INPUT_POST, "name"));

        $module_import_file = DATA_PATH. "/runtime/module.php";
        if(!is_writable(dirname($module_import_file))) ajax_result("error", "缓存目录权限错误，请确保 " . DATA_PATH . " 目录有写入权限！");

        $module_file = MODULE_PATH. DIRECTORY_SEPARATOR. $name . DIRECTORY_SEPARATOR . "module.php";
        if(!file_exists($module_file)) ajax_result("error", "没有找到模块入口文件，安装失败！");
        require_once $module_file;

        $module_class_name = "_". ucfirst($name) . "ModuleClass";
        $interface = new ReflectionClass("_module_interface");
        $module_class = new ReflectionClass($module_class_name);
        if(!$module_class->isSubclassOf($interface)) ajax_result("error", "模块入口文件格式错误！");
        $module = $module_class->newInstance();
        $method = $module_class->getMethod("install");
        $result = $method->invoke($module);
        if($result !== true) ajax_result("error", $result);

        // 安装完成写入设置
        $_M->db();
        $row = $_M->db->get_row(array("table"=>"options", "field"=>"options_value", "where"=>"type='system' and options_name='module_installed' and category='hide'"));
        $installed = array();
        if(false === $row) {        // 没有设置内容
            $_M->db->insert($this->_options_data_defined(), array("table"=>"options"));
        } else {
            $installed = json_decode($row["options_value"], true);
        }
        if(!in_array($name, $installed)) $installed[] = $name;
        $_M->db->update(array("options_value"=>json_encode($installed)), array("table"=>"options", "field"=>"options_value", "where"=>"type='system' and options_name='module_installed' and category='hide'"));
        // 删除临时配置文件
        unlink($module_import_file);

        ajax_result("successful");
    }

    function _remove(){
        global $_M;

        $code = filter_input(INPUT_POST, "code");
        if($code != $_SESSION["tamperproof_verify_code"]) ajax_result("error", "验证错误！");
        $name = clear_string(filter_input(INPUT_POST, "name"));
        $retain_data = filter_input(INPUT_POST, "retain_data", FILTER_VALIDATE_BOOLEAN) ? 1 : 0;

        $module_import_file = DATA_PATH. "/runtime/module.php";
        if(!is_writable(dirname($module_import_file))) ajax_result("error", "缓存目录权限错误，请确保 " . DATA_PATH . " 目录有写入权限！");

        $module_class_name = "_". ucfirst($name) . "ModuleClass";
        $interface = new ReflectionClass("_module_interface");
        $module_class = new ReflectionClass($module_class_name);
        if(!$module_class->isSubclassOf($interface)) ajax_result("error", "模块入口文件格式错误！");
        $module = $module_class->newInstance();
        $method = $module_class->getMethod("uninstall");
        $result = $method->invoke($module, $retain_data);
        if($result !== true) ajax_result("error", $result);

        // 卸载完成写入设置
        $_M->db();
        $row = $_M->db->get_row(array("table"=>"options", "field"=>"options_value", "where"=>"type='system' and options_name='module_installed' and category='hide'"));
        $installed = array();
        if(false === $row) {        // 没有设置内容
            $_M->db->insert($this->_options_data_defined(), array("table"=>"options"));
        } else {
            $installed = json_decode($row["options_value"], true);
        }
        if(in_array($name, $installed)) unset($installed[array_search($name, $installed)]);
        $_M->db->update(array("options_value"=>json_encode($installed)), array("table"=>"options", "field"=>"options_value", "where"=>"type='system' and options_name='module_installed' and category='hide'"));
        // 删除临时配置文件
        unlink($module_import_file);

        ajax_result("successful");
    }
}